//
//  Flow.m
//  Core
//
//  Created by Cuong Doan on 3/7/13.
//  Copyright (c) 2013 Cuong Doan. All rights reserved.
//

#import "Flow.h"

@interface Flow()

-(void)getNextOrder;

-(void)getDataByID:(int)userID andOrder:(int)flowOder;

@end

@implementation Flow
{
    sqlite3 *db;
}

-(id)initWithID:(int)userID andOrder:(int)flowOrder
{
    self = [super init];
    
    @try
    {
        [self getDataByID:userID andOrder:flowOrder];
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }    
    
    return self;
}

-(void)getDataByID:(int)userID andOrder:(int)flowOder
{
    sqlite3_stmt *statement;
    
    const char *sql = "select * from FlowInput where User_ID = ? and FlowInputOrder = ?";
    
    @try
    {
        [DatabaseManager openAndPrepareDatabase:&db andStatement:&statement andSQL:sql];
        
        [DatabaseManager bindInt:statement andValue:userID andIndex:1];
        
        [DatabaseManager bindInt:statement andValue:flowOder andIndex:2];
        
        while(sqlite3_step(statement) == SQLITE_ROW)
        {
            self.UserID = sqlite3_column_int(statement, 0);
            
            self.FlowInputOrder = sqlite3_column_int(statement, 1);
            
            self.Description = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
            
            self.Amount = sqlite3_column_double(statement, 3);
            
            self.FlowType = sqlite3_column_int(statement, 4);
            
            self.CatID = sqlite3_column_int(statement, 5);
            
            self.FlowDate = [DatabaseManager columnDate:statement andIndex:6];
        }
        
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }
    @finally
    {
        sqlite3_close(db);
    }
}

-(void)getNextOrder
{
    sqlite3_stmt *statement;
    
    const char *sql = "select (ifnull(max(FlowInputOrder), 0) + 1) from FlowInput where User_ID = ?";
    
    @try
    {
        [DatabaseManager prepareDatabase:db andStatement:&statement andSQL:sql];
        
        [DatabaseManager bindInt:statement andValue:self.UserID andIndex:1];
        
        while(sqlite3_step(statement) == SQLITE_ROW)
        {
            self.FlowInputOrder = sqlite3_column_int(statement, 0);
        }
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }    
}

-(void)insertFlow
{
    sqlite3_stmt *statement;
    
    const char *sql = "insert into FlowInput (User_ID, FlowInputOrder, Description, Amount, FlowType, Cat_ID, FlowDate) values (?, ?, ?, ?, ?, ? ,?";
    
    @try
    {
        [self getNextOrder];
        
        [DatabaseManager openAndPrepareDatabaseV2:&db andStatement:&statement andSQL:sql];
        
        [DatabaseManager bindInt:statement andValue:self.UserID andIndex:1];
        
        [DatabaseManager bindInt:statement andValue:self.FlowInputOrder andIndex:2];
        
        [DatabaseManager bindText:statement andValue:self.Description andIndex:3];
        
        [DatabaseManager bindDouble:statement andValue:self.Amount andIndex:4];
        
        [DatabaseManager bindInt:statement andValue:self.FlowType andIndex:5];
        
        [DatabaseManager bindInt:statement andValue:self.CatID andIndex:6];
        
        [DatabaseManager bindDate:statement andValue:self.FlowDate andIndex:7];
        
        sqlite3_step(statement);
        
        sqlite3_finalize(statement);
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }
    @finally
    {
        sqlite3_close(db);
    }
}

-(void)updateFlow
{
    sqlite3_stmt *statement;
    
    const char *sql = "update FlowInput set Description = ?, Amount = ?, FlowType = ?, Cat_ID = ?, FlowDate = ? where User_ID = ? and FlowInputOrder = ?";
    
    @try
    {
        [DatabaseManager openAndPrepareDatabaseV2:&db andStatement:&statement andSQL:sql];
        
        [DatabaseManager bindText:statement andValue:self.Description andIndex:1];
        
        [DatabaseManager bindDouble:statement andValue:self.Amount andIndex:2];
        
        [DatabaseManager bindInt:statement andValue:self.FlowType andIndex:3];
        
        [DatabaseManager bindInt:statement andValue:self.CatID andIndex:4];
        
        [DatabaseManager bindDate:statement andValue:self.FlowDate andIndex:5];
        
        [DatabaseManager bindInt:statement andValue:self.UserID andIndex:6];
        
        [DatabaseManager bindInt:statement andValue:self.FlowInputOrder andIndex:7];
        
        sqlite3_step(statement);
        
        sqlite3_finalize(statement);
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }
    @finally
    {
        sqlite3_close(db);
    }
}

@end
